package graphs;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Displays a bar graph of a name's popularity.
 */
@SuppressWarnings("serial")
public class LineGraph extends AbstractNumericalPlot
{

    /**
     * Constructor for Line Graph
     */
    public LineGraph()
    {
        myData = null;
        myLabels = null;
        myXvals = null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see graphs.AbstractGraph#draw(java.util.Map, java.util.List)
     */

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    public void paintComponent(Graphics pen)
    {
        Dimension bounds = this.getSize();
        if (myLabels.size() > 1)
            pen.drawString(myLabels.get(1), bounds.width - 10, bounds.height);
        if (myLabels.size() > 0)
            pen.drawString(myLabels.get(0), (bounds.width / 2), 20);
        List<Color> colors = new ArrayList<Color>();
        Map<Integer, List<Point>> points = new TreeMap<Integer, List<Point>>();
        double[] yMinMax = findYMinMax(myData, myXvals, colors);
        double yMax = yMinMax[0];
        double yMin = yMinMax[1];
        double[] xMinMax = findXMinMax(myData, myXvals);
        double xMax = xMinMax[0];
        double xMin = xMinMax[1];

        drawGraph(pen, bounds, colors, points, yMax, xMax, yMin, xMin, myXvals,
                myData, myLabels);
    }

    /*
     * (non-Javadoc)
     * 
     * @see graphs.AbstractNumericalPlot#plotPoints(java.awt.Graphics,
     * java.util.Map, int, int, java.awt.Point, java.awt.Dimension)
     */
    protected void plotPoints(Graphics pen, Map<Integer, List<Point>> points,
            int count, int i, Point dot1, Dimension size)
    {
        if (count == 0)
        {
            pen.fillOval(dot1.x - 3, dot1.y - 3, 7, 7);
            if (points.get(count) == null)
            {
                List<Point> newList = new ArrayList<Point>();
                newList.add(i, dot1);
                points.put(count, newList);
            }
            else
                points.get(count).add(i, dot1);
        }
        if (count > 0 && i < points.get(count - 1).size())
        {
            pen.fillOval(dot1.x - 3, dot1.y - 3, 7, 7);
            pen.drawLine(points.get(count - 1).get(i).x, points.get(count - 1)
                    .get(i).y, dot1.x, dot1.y);
            if (points.get(count) == null)
            {
                List<Point> newList = new ArrayList<Point>();
                newList.add(i, dot1);
                points.put(count, newList);
            }
            else
                points.get(count).add(i, dot1);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see graphs.AbstractGraph#toString()
     */
    public String toString()
    {
        return "LineGraph";
    }
}